layui.use(['form', 'jquery'], function () {
    let form = layui.form;
    let $ = layui.jquery;

    // 提交按钮事件
    form.on('submit(lease-save-btn-filter)', function (data) {
        let field = data.field;
        let oldPwd = field['lease-old-pwd-input-name'];
        let newPwd = field['lease-new-pwd-input-name'];
        let conPwd = field['lease-confirm-pwd-input-name'];

        // 校验当前密码
        if ($.trim(oldPwd).length === 0) {
            $('#lease-old-pwd-tips-div-id').text('当前密码不能为空！');
            $('#lease-old-pwd-input-id').focus();
            return false;
        }
        if (!/^[a-zA-Z0-9]+$/.test(oldPwd)) {
            $('#lease-new-pwd-tips-div-id').text('新密码包含非法字符！');
            $('#lease-new-pwd-input-id').focus();
            return false;
        }
        // 校验新密码
        if ($.trim(newPwd).length === 0) {
            $('#lease-new-pwd-tips-div-id').text('新密码不能为空！');
            $('#lease-new-pwd-input-id').focus();
            return false;
        }
        if (newPwd.length < 6 || newPwd.length > 16) {
            $('#lease-new-pwd-tips-div-id').text('新密码长度必须在6-16位之间！');
            $('#lease-new-pwd-input-id').focus();
            return false;
        }
        if (!/^[a-zA-Z0-9]+$/.test(newPwd)) {
            $('#lease-new-pwd-tips-div-id').text('新密码包含非法字符！');
            $('#lease-new-pwd-input-id').focus();
            return false;
        }
        if (oldPwd === newPwd) {
            $('#lease-new-pwd-tips-div-id').text('新密码不能与当前密码相同！');
            $('#lease-new-pwd-input-id').focus();
            return false;
        }

        // 校验确认密码
        if ($.trim(conPwd).length === 0) {
            $('#lease-confirm-pwd-tips-div-id').text('确认新密码不能为空！');
            $('#lease-confirm-pwd-input-id').focus();
            return false;
        }
        if (newPwd !== conPwd) {
            $('#lease-confirm-pwd-tips-div-id').text('确认新密码与新密码不一致！');
            $('#lease-confirm-pwd-input-id').focus();
            return false;
        }

        // 发送 AJAX 请求保存新密码
        $.ajax({
            url: '/user/save-password.do',
            type: 'post',
            dataType: 'json',
            data: { oldPassword: oldPwd, newPassword: newPwd },
            success: function (message) {
                let msgCode = message['msgCode'];
                if (msgCode == message['success']) {
                    layer.msg('密码修改成功！', { icon: 1 });
                } else if (msgCode == message['failure']) {
                    layer.msg('密码修改失败！', { icon: 2 });
                }
            },
            error: function () {
                layer.msg('系统繁忙', { icon: 5 });
            },
            complete: function () {
                form.val('lease-change-pwd-form-filter', {
                    'lease-old-pwd-input-name': '',
                    'lease-new-pwd-input-name': '',
                    'lease-confirm-pwd-input-name': ''
                });
                $('#lease-old-pwd-tips-div-id').text('');
                $('#lease-new-pwd-tips-div-id').text('6-12位数字或字母');
                $('#lease-confirm-pwd-tips-div-id').text('');
            }
        });
        return false;
    });

    // 当前密码输入框失去焦点事件
    $('#lease-old-pwd-input-id').blur(function () {
        let oldPassword = form.val('lease-change-pwd-form-filter')['lease-old-pwd-input-name'];
        oldPassword = $.trim(oldPassword);
        if (oldPassword.length === 0) {
            $('#lease-old-pwd-tips-div-id').text('当前密码不能为空！');
            $('#lease-old-pwd-input-id').focus();
            return;
        }
        // 发送 AJAX 验证当前密码
        $.ajax({
            url: '/user/verify-old-password.do',
            type: 'post',
            dataType: 'json',
            data: { oldPassword: oldPassword },
            success: function (message) {
                let msgCode = message['msgCode'];
                if (msgCode == message['success']) {
                    $('#lease-old-pwd-tips-div-id').text('密码正确！');
                } else if (msgCode == message['failure']) {
                    $('#lease-old-pwd-tips-div-id').text('当前密码不正确！');
                    form.val('lease-change-pwd-form-filter', {
                        'lease-old-pwd-input-name': ''
                    });
                }
            },
            error: function () {
                layer.msg('系统繁忙', { icon: 5 });
            }
        });
    });
});